<?php
/* 
 * @author Jens Beyer
 * @date 04-09-2012
 */
class Ajax_Model_Tabs_InsertTab_Russian extends Ajax_Model
{
    protected $_russianData;
    
    public function insert()
    {
        $q = "INSERT INTO russian SET subject='"
            . $_REQUEST["subject"] . "'"
            . ", body='"
            . $_REQUEST["body"] . "'"
        ;
        $this->getDb()->execute($q);
    }

    public function getAllRussianData()
    {
        $q = 'SELECT * FROM russian';
        $russianDataQuery = $this->getDb()->queryArray($q);
        $russianData = array();
        $this->_russianData = array();
        if (!empty($russianDataQuery)) {
            foreach($russianDataQuery as $key => $row) {
                $russianData[$key]['subject'] = $row['subject'];
                $russianData[$key]['body'] = $row['body'];
            }
        }
        $this->_russianData = $russianData;
        return $this->_russianData;
    }

    public function fetch()
    {
        if ('insert' == $_REQUEST['subaction']) {
            $this->insert();
        }
        return $this;
    }

    /**
     *
     * @return string
     */
    public function returnAjax()
    {
        $russianData = $this->getAllRussianData();
        $this->getAt()->assign('russianData', $russianData);
        $content = $this->getAt()->fetch('Tabs/InsertTab/Russian/data.tpl');
        $navigationToDiv = str_replace("Ajax_Model_Tabs_", "", get_class($this));
        $navigationToDiv = '#' . str_replace("_", " > #", $navigationToDiv);
//        $divInformation = "#" .
//            . " > #information"
//        ;
        $fetchedData = array();
        $templateData = array();
        $templateData['navigationToDiv'] = $navigationToDiv;
        $templateData['divToReplace'] = '#data';
        $templateData['content'] = $content;
        $fetchedData[] = $templateData;
        ob_start();
        print_r('$fetchedData = ');
        print_r($fetchedData);
        print_r("\n");
        $text = ob_get_clean();
        error_log($text);
        $fetchedDivDataJson = json_encode($fetchedData);
        return $fetchedDivDataJson;
    }

    /**
     * @return Smarty
     */
    public function getAt()
    {
        if (!$this->_at) {
            $this->_at = Controller::getActionTemplate();
        }
        return $this->_at;
    }

    /**
     *
     * @param Smarty $at
     */
    public function setAt(Smarty $at)
    {
        $this->_at = $at;
    }
    
    /**
     *
     * @return MyDb
     */
    public function getDb()
    {
        if (!$this->_db) {
            $this->_db = Controller::getDatabase("localhost");
        }
        return $this->_db;
    }

    /**
     *
     * @param Db $db
     */
    public function setDb(Db $db)
    {
        $this->_db = $db;
    }
}